package lcr;

import java.util.ArrayList;
import java.util.List;

public class Test0015 {
}

class Solution0015_1 {
    public List<Integer> findAnagrams(String s, String p) {
        ArrayList<Integer> ans = new ArrayList<>();
        int l=0, r=0,flag=p.length(),len=s.length();
        if (len<flag) return ans;

        char[] charArray = s.toCharArray();
        int[] records = new int[26];
        for (char c : p.toCharArray()) {
            records[c - 'a']++;
        }

        for (; r < p.length(); r++){
            char c = charArray[r];
            int re = --records[c - 'a'];

            if (re>=0) flag--;
            else flag++;
        }

        for (; r < len; r++,l++){
            if (flag==0) ans.add(l);

            int reR = --records[charArray[r] - 'a'];
            int reL = ++records[charArray[l] - 'a'];
            if (reR>=0) flag--;
            else flag++;
            if (reL>0) flag++;
            else flag--;
        }

        if (flag==0) ans.add(l);
        return ans;
    }
}
